package com.coocaa.ops.plus.dashboard.v1.codegen.validator;

import com.coocaa.ops.plus.dashboard.v1.codegen.constant.SqlConstants;
import com.coocaa.ops.plus.dashboard.v1.codegen.convert.ColumnListConvert;
import com.coocaa.ops.plus.dashboard.v1.codegen.model.CodeGenConfigModel;
import com.coocaa.ops.plus.dashboard.v1.codegen.model.TableInfoModel;
import org.apache.commons.lang3.StringUtils;
import org.springframework.util.Assert;

import java.util.List;
import java.util.Objects;

/**
 * @author : bijiahao
 * @date : 2019/6/5.
 * @description:
 */
public class CodeGenValidator {

    /**
     * 是否 本地运行
     *
     * @param codeGenConfigModel
     * @return
     */
    public static boolean isUseProjectPath(CodeGenConfigModel codeGenConfigModel) {
        return Objects.equals(1, codeGenConfigModel.getUseProjectPath());
    }

    public static boolean isOriginTableOnly(CodeGenConfigModel codeGenConfigModel) {
        return codeGenConfigModel.getOriginTableOnly() != null && codeGenConfigModel.getOriginTableOnly() == 1;
    }

    public static  void valid(CodeGenConfigModel codeGenConfigModel){
        Assert.notEmpty(codeGenConfigModel.getTableList(), "表配置信息不能为空");
        if (isOriginTableOnly(codeGenConfigModel)){
            List<TableInfoModel>  tableInfoModelList = codeGenConfigModel.getTableList();
            for(TableInfoModel tableInfoModel : tableInfoModelList){
                Assert.isTrue(isFitOriginTable(tableInfoModel), "不符合带副本的表结构");
                ColumnListConvert.filterColumns(tableInfoModel);
            }
        }
    }

    private static boolean isFitOriginTable(TableInfoModel tableInfoModel){
        return StringUtils.isNotBlank(tableInfoModel.getBaseColumnsStr())
                && isAllContain(tableInfoModel.getBaseColumnsStr())
                ;

    }

    private static boolean isAllContain(String baseColumnsStr){
        for(String column : SqlConstants.SUPER_ENTITY_COLUMNS_ORIGIN_TABLE){
            if (!baseColumnsStr.contains(column)){
                return false;
            }
        }
        return true;
    }
}
